package com.webserver_v4.controller;

import com.webserver_v4.entity.User;
import com.webserver_v4.http.HttpServletRequest;
import com.webserver_v4.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;

public class UserController {
        private static File userDir;

        static {
            userDir = new File("./users");
            if (!userDir.exists()) {
                userDir.mkdirs();
            }
        }


        public void reg(HttpServletRequest request, HttpServletResponse response) {
            System.out.println("开始处理注册!!!!");
            //获取表单信息
            String username = request.getParameters("username");
            String password = request.getParameters("password");
            String nickname = request.getParameters("nickname");
            String ageStr = request.getParameters("age");
            //必要验证
            if (username == null || username.isEmpty() ||
                    password == null || password.isEmpty() ||
                    nickname == null || nickname.isEmpty() ||
                    ageStr == null || ageStr.isEmpty() || !ageStr.matches("[0-9]+")
            ) {
                //提示信息输入有误
                response.sendRedirect("reg_info_error.html");
                return;
            }

            System.out.println(username + "," + password + "," + nickname + "," + ageStr);
            int age = Integer.parseInt(ageStr);
            User user = new User(username, password, nickname, age);
            File file = new File(userDir, username + ".obj");
            if (file.exists()) {//文件存在则说明该用户已经注册过了
                response.sendRedirect("have_user.html");
                return;
            }
            try (
                    FileOutputStream fos = new FileOutputStream(file);
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
            ) {
                oos.writeObject(user);
                //利用响应对象要求浏览器访问注册成功页面
               response.sendRedirect("reg_success.html");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        public void login(HttpServletRequest request,HttpServletResponse response){
            String username = request.getParameters("username");
            String password = request.getParameters("password");

            if (username==null||username.isEmpty()||
                password==null||password.isEmpty()
            ){
                response.sendRedirect("/login_info_error.html");
                return;
            }
            File file = new File(userDir,username+".obj");
            if (file.exists()){
                try (
                        FileInputStream fis = new FileInputStream(file);
                        ObjectInputStream ois = new ObjectInputStream(fis);
                        ){
                    User user = (User) ois.readObject();
                    if (user.getPassword().equals(password)){
                        response.sendRedirect("/login_success.html");
                        return;
                    }
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            response.sendRedirect("/login_fail.html");
        }
    }
